#include "SocketHandle.h"
#include "UserController.h"

namespace XIAOO{

    std::string SocketHandle::onReadHandle(int sockfd, std::string readBuffer){
        Json::Value root, returnRoot;
        //老版本字符串转json
        // Json::Reader reader;
        // reader.parse(readBuffer, root);
        //新版本字符串转json
        JSONCPP_STRING err;
        Json::CharReaderBuilder readerBuilder;
        Json::CharReader* reader(readerBuilder.newCharReader());
        if(! reader->parse(readBuffer.c_str(), readBuffer.c_str() + readBuffer.length(), &root, &err) ){
            std::cout << "parse json error" << std::endl;
        }

        //解析出控制器和方法
        this->route = root["route"].asString();
        std::string controller, action;
        this->parseControllerAndAction(&controller, &action);
        
        returnRoot = this->routeMap(root, controller, action, sockfd);

        //返回方式1
        //包含格式化（\n和\t）的json
        Json::StreamWriterBuilder builder;
        std::string writeBuffer = Json::writeString(builder, returnRoot);

        //返回方式2
        //直接返回一行json，没有格式化，也就是没有\n\t等转义符
        // Json::FastWriter writer;
        // const std::string writeBuffer = writer.write(returnRoot);

        return writeBuffer;
    }

    void SocketHandle::onWriteHandle(int sockfd, std::string writeBuffer){
        
        int writeBufferSize = writeBuffer.length() + 1;
        write(sockfd, (char*)writeBuffer.c_str(), writeBufferSize);
        std::cout <<"【服务器返回begin】" << std::endl;
        printf((char*)writeBuffer.c_str());
        std::cout <<"\n【服务器返回end】" << std::endl;
    }

    void SocketHandle::onOfflineHandle(int sockfd){
        UserController user;
        user.offLine(sockfd);
    }

    Json::Value SocketHandle::routeMap(Json::Value root, std::string controller, std::string action, int sockfd){
        Json::Value returnRoot;
        if(controller == "user"){
            UserController user;
            if(action == "registe"){
                returnRoot = user.registe(root);
            }else if(action == "login"){
                returnRoot = user.login(root, sockfd);
            }else if(action == "addFriend"){
                returnRoot = user.addFriend(root);
            }else if(action == "friendList"){
                returnRoot = user.friendList(root);
            }else if(action == "chat"){
                returnRoot = user.chat(root, sockfd);
            }else if(action == "getUserInfo"){
                returnRoot = user.getUserInfo(root);
            }else{
                returnRoot["code"] = -1;
                returnRoot["msg"] = "no such action";
            }
        }else{
            returnRoot["code"] = -1;
            returnRoot["msg"] = "no such controller";
        }

        returnRoot["route"] = this->route + "Callback";

        return returnRoot;
    }

    void SocketHandle::parseControllerAndAction(std::string *controller, std::string *action){
        std::string route = this->route;
        std::string subStr = "/";
        int pos = route.find(subStr, 0);
        *controller = route.substr(0, pos);
        *action = route.substr(pos + subStr.length(), route.length());
    }

    
    
    SocketHandle::SocketHandle(){
        
    }

    SocketHandle::~SocketHandle(){
        
    }
}